package com.abbas.abchat.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.abbas.abchat.R;
import com.abbas.abchat.entity.ChatTable;

import java.util.List;

public class SearchAccountActivity extends BaseActivity {

    private ImageButton ibBack;
    private EditText etSearch;
    private ImageButton ibSearch;
    private LinearLayout findInNet;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_account;
    }

    @Override
    protected void init() {
        ibBack = (ImageButton) findViewById(R.id.ib_back);
        etSearch = (EditText) findViewById(R.id.et_search);
        ibSearch = (ImageButton) findViewById(R.id.ib_search);
        findInNet = (LinearLayout) findViewById(R.id.find_in_net);
    }

    @Override
    protected void initEvent() {
        ibBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishThis();
            }
        });

        ibSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startSearch();
            }
        });

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH){
                    startSearch();
                }
                return false;
            }
        });

        findInNet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startSearch();
            }
        });

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0){
                    findInNet.setVisibility(View.VISIBLE);
                }else {
                    findInNet.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void startSearch(){
        String targetStr = etSearch.getText().toString().trim();
        if (!TextUtils.isEmpty(targetStr)){
            Intent intent = new Intent(SearchAccountActivity.this, SearchAccountResultActivity.class);
            intent.putExtra("target",targetStr);
            startActivity(intent);
        }
    }

}